define(['./FormPlugin', '../../LayoutInfo', './ActionJScript'], function(FormPlugin, LayoutInfo, actionJScript) {
    'use strict';

    var FN_ARGS = /\s*new Array\s*[^\(]*\(\s*([^\)]*)\)/m;
    var FN_ARG_SPLIT = /,/;    

    function setStyleLTWH($obj, l, t, w, h) {        
        $obj.css({
            'left': l,
            'top': t,
            'width': w,
            'height': h
        });        
    }

    var formFieldHighlightClass = 'fwr-form-field-highlight';
    var requiredFieldClass = 'fwr-form-require-field-highlight';
    var formListItemSelectedClass = 'fwr-form-listitem-selected'; 

    function transferPage(param, scale, pixels) {
        if (!pixels){
            pixels = LayoutInfo.pixelsPerPoint;
        }
        return Math.round(param * pixels * scale);
    }

    var FormWidgetDirective = {};
    FormWidgetDirective.extend = function(options){      
        return ['$compile', 'FormService', function($compile, FormService){        
            return {
                restrict: 'EA',
                replace: true,
                templateUrl: options.templateUrl,
                link: function($scope, element, attr){
                    var thisWidget = {};                   
                    options.link.call(thisWidget, $scope, element, attr, $compile, FormService);
                   
                    thisWidget.form_fg.addClass($scope.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass); 

                    $scope.$watch('formWidget.onfocus', function(newValue, oldValue) {
                        if (newValue === oldValue) return;
                        if (newValue) {
                            thisWidget.form_fg.hide();
                        } else {
                            thisWidget.form_fg.show();
                        }
                    });

                    $scope.$watch('bHighlight', function(newValue, oldValue) {                        
                        if (newValue) {
                            thisWidget.form_fg.show();
                        } else if ($scope.formWidget.bReadOnly) {
                            thisWidget.form_fg.hide();
                        }
                    });
                    
                    element.on("layout", function() {
                        options.layout.call(thisWidget, $scope);
                    });

                    FormService.onItemValueChange($scope.formWidget.name, function(data) {
                        if ($.isFunction(options.update)){                            
                            options.update.call(thisWidget, data);
                        }
                    });
                    options.layout.call(thisWidget, $scope);                                   
                }
            };
        }];
    };
   
    FormPlugin.directive('formCheckBox', FormWidgetDirective.extend({
        templateUrl: 'js/Plugins/Form/tpl/Form_CheckBox.html',
        link: function($scope, element, attr, fromService) {
            this.element = element;
            this.form_bg = element.find(".fwr-form-bg");
            this.form_fg = element.find(".fwr-form-fg");
            this.form_bg_img = element.find("img");   
            this.check_on_element = element.find("img.check-on");
            this.check_off_element = element.find("img.check-off");                                          

            var self = this;
            $scope.$watch('formWidget.bCheck', function(newValue, oldValue) {
                if (newValue === oldValue) return;
                if (newValue){
                    self.check_on_elment.show();
                    self.check_off_element.hide();
                }
                else{
                    self.check_on_element.hide();
                    self.check_off_element.show();
                }
            });                          
        },
        layout: function($scope) {
            var formWidget = $scope.formWidget;
            var scale = $scope.scale;
            var ltwh = {
                left : transferPage(formWidget.rect.left, scale),
                top : transferPage(formWidget.rect.top, scale),
                width : transferPage(formWidget.rect.width, scale),
                height : transferPage(formWidget.rect.height, scale)
            };
            this.element.css(ltwh);          
            var imgWidth = transferPage(formWidget.formPage.allImgWidth, scale, 1);
            var imgHeight = transferPage(formWidget.formPage.allImgHeight, scale, 1);      
            var img_off_left = 0 - transferPage(formWidget.offImgPos, scale, 1);       
            setStyleLTWH(this.check_off_element, img_off_left, 0, imgWidth, imgHeight);

            var img_on_left = 0 - transferPage(formWidget.onImgPos, scale, 1);                                  
            setStyleLTWH(this.check_on_element, img_on_left, 0, imgWidth, imgHeight);     
        }
    }));
    

    FormPlugin.directive('formComboBox', FormWidgetDirective.extend({          
        templateUrl: 'js/Plugins/Form/tpl/Form_ComboBox.html',
        link: function($scope, element, attr, formService) {
            this.form_bg = element.find(".fwr-form-bg");
            this.combo_list = element.find(".fwr-form-widget-combo-list");
            this.form_fg = element.find(".fwr-form-fg");
            this.form_bg_img = element.find("img");
            this.form_input = element.find("input");
            this.form_list = element.find(".fwr-form-widget-combo-list");    

             $scope.$watch('formWidget.bCheck', function(newValue, oldValue) {
                if (newValue === oldValue) return;
                if (newValue){
                    this.check_on.show();
                    this.check_off.hide();
                }
                else{
                    this.check_on.hide();
                    this.check_off.show();
                }
            });    
        },
        layout: function($scope) {
            var formWidget = $scope.formWidget;
            var scale = $scope.scale;
            var bg_ltwh = {
                left: transferPage(formWidget.rect.left , scale),
                top: transferPage(formWidget.rect.top , scale),
                width: transferPage(formWidget.rect.width , scale),
                height: transferPage(formWidget.rect.height, scale),
            };

            var divBorderWidth = transferPage(formWidget.borderWidth, scale);

            var control_ltwh = {
                left: bg_ltwh.left + divBorderWidth,
                top: bg_ltwh.top + divBorderWidth,
                width: bg_ltwh.width - 2 * divBorderWidth,
                height: bg_ltwh.height - 2 * divBorderWidth
            };

            var listDivTop = bg_ltwh.top + bg_ltwh.height;
            var divFontSize = 0;
            var listFontSize = 0;
            if (formWidget.fontSize != -1) {
                divFontSize = transferPage(formWidget.fontSize, scale);
                listFontSize = divFontSize;
            } else {
                if (formWidget.formPage.rotate == 1 || formWidget.formPage.rotate == 3) {
                    divFontSize = transferPage(formWidget.rect.width * 0.7, scale);
                } else {
                    divFontSize = transferPage(formWidget.rect.height * 0.7, scale);
                }
                listFontSize = transferPage(12, scale);
            }
            this.form_bg.css(bg_ltwh);
            this.combo_list.css(control_ltwh).css('line-height', control_ltwh.height + 'px').css('font-size', divFontSize + 'px');
            //form_fg.css(ltwh);
            //
            var input_ltwh = control_ltwh;
            var rightWidth = transferPage(16, scale);

            switch (formWidget.formPage.rotate) {
                case 1: //90                            
                    input_ltwh.height -= rightWidth;
                    break;
                case 2: //180                            
                    input_ltwh.width -= rightWidth;
                    input_ltwh.left += rightWidth;
                    break;
                case 3: //270                            
                    input_ltwh.height -= rightWidth;
                    input_ltwh.top += rightWidth;
                    break;
                default: //0                            
                    input_ltwh.width -= rightWidth;
            }

            this.form_input.css('line-height', input_ltwh.height + 'px').css('font-size', divFontSize + 'px');
            this.form_input.css(input_ltwh);

            this.form_list.css('left', bg_ltwh.left + 'px').css('top', bg_ltwh.top + 'px').css('width', bg_ltwh.width + 'px');

            this.form_list.children().css('font-size', listFontSize + 'px');
            this.form_fg.css(control_ltwh);

            var imgLeft = 0 - transferPage(formWidget.imgPos , scale, 1);
            var imgWidth = transferPage(formWidget.formPage.allImgWidth , scale, 1);
            var imgHeight = transferPage(formWidget.formPage.allImgHeight, scale, 1);
            this.form_bg_img.css({
                "left": imgLeft,
                "top": 0,
                "width": imgWidth,
                "height": imgHeight
            });
        }
    }));   

    FormPlugin.directive('formRadioButton', FormWidgetDirective.extend({
        templateUrl: 'js/Plugins/Form/tpl/Form_RadioButton.html',
        link: function($scope, element, attr, $compile, formService) {
            this.element = element;
            this.form_bg = element.find(".fwr-form-bg");
            this.form_fg = element.find(".fwr-form-fg");
            this.form_bg_img = element.find("img");   
            this.check_on_element = element.find("img.check-on");
            this.check_off_element = element.find("img.check-off");    
        },
        layout: function($scope) {          
            var formWidget = $scope.formWidget;
            var scale = $scope.scale;
            var ltwh = {};
            ltwh.left = transferPage(formWidget.rect.left, scale);
            ltwh.top = transferPage(formWidget.rect.top, scale);
            ltwh.width = transferPage(formWidget.rect.width, scale);
            ltwh.height = transferPage(formWidget.rect.height, scale);
            this.element.css(ltwh);     
            var imgWidth = transferPage(formWidget.formPage.allImgWidth, scale, 1);
            var imgHeight = transferPage(formWidget.formPage.allImgHeight,scale, 1);      
            var img_off_left = 0 - transferPage(formWidget.offImgPos, scale, 1);       
            setStyleLTWH(this.check_off_element, img_off_left, 0, imgWidth, imgHeight);

            var img_on_left = 0 - transferPage(formWidget.onImgPos * scale, 1);     
            setStyleLTWH(this.check_on_element, img_on_left, 0, imgWidth, imgHeight);
        }    
    }));   
   
    function createListOptionEle($compile, $scope, option, wrapperEle) {
        var template = '<div tabindex="2" list-option style="overflow: hidden; display: block; width: 100%; cursor: default;" isselected="0"></div>';
        var newScope = $scope.$new();
        newScope.option  = option;
        var optionEle = $compile(template)(newScope);     
        optionEle.attr("realvalue", option.realValue).html(option.text);
        newScope.$watch('option.bSelected', function(newValue, oldValue){
            if (newValue){
                 optionEle.addClass(formListItemSelectedClass);
            }
            else{
                optionEle.removeClass(formListItemSelectedClass);
            }
        });        
        wrapperEle.append(optionEle);
    }
    FormPlugin.directive('formListBox', FormWidgetDirective.extend({
        templateUrl: 'js/Plugins/Form/tpl/Form_ListBox.html',              
        link: function($scope, element, attr, $compile, formService) {
            this.element = element;
            this.form_bg = element.find(".fwr-form-bg");
            this.form_fg = element.find(".fwr-form-fg");
            this.form_bg_img = element.find("img");         
            this.option_wrapper = element.find(".option-wrapper");   
            var formWidget = $scope.formWidget; 
            var self = this;          

            angular.forEach(formWidget.options, function(option){
                createListOptionEle($compile, $scope, option, self.option_wrapper);
            });                     
        },
        layout : function($scope) {            
            var formWidget = $scope.formWidget;
            var scale = $scope.scale;
            var ltwh = {};
            ltwh.left = transferPage(formWidget.rect.left, scale);
            ltwh.top = transferPage(formWidget.rect.top, scale);
            ltwh.width = transferPage(formWidget.rect.width, scale);
            ltwh.height = transferPage(formWidget.rect.height, scale);
            this.element.css(ltwh);        
          

            var divBorderWidth = transferPage(formWidget.borderWidth, scale);
            setStyleLTWH(this.option_wrapper, divBorderWidth, divBorderWidth, ltwh.width - 2 * divBorderWidth, ltwh.height - 2 * divBorderWidth);

            var imgPos = 0 - transferPage(formWidget.imgPos, scale, 1);
            var imgWidth = transferPage(formWidget.formPage.allImgWidth, scale, 1);
            var imgHeight = transferPage(formWidget.formPage.allImgHeight, scale, 1);      
            this.form_bg_img.css({
                "left": imgPos,
                "top": 0,
                "width": imgWidth,
                "height": imgHeight
            });                   

            var divFontSize = 0;
            if (formWidget.fontSize != -1) {
                divFontSize = transferPage(formWidget.fontSize, scale);
            } else {
                divFontSize = transferPage(12 , scale);
            }

            this.option_wrapper.children().css('font-size', divFontSize + 'px');
        },
        update: function(fieldValue){          
            if (!fieldValue || fieldValue === "") return;
            var valueArr = fieldValue.split(';');
            this.option_wrapper.children().each(function(){
                var select = false;
                var scope = $(this).scope();
                if ($.inArray(scope.option.realValue, valueArr) != -1){
                    select = true;    
                }               
                else{
                    select = false;
                }               
                scope.$evalAsync(function(scope) {
                    scope.option.bSelected = select;
                }); 
            });
        }
    }));      

    FormPlugin.directive('formText', FormWidgetDirective.extend({     
        templateUrl: 'js/Plugins/Form/tpl/Form_Text.html',
        link: function($scope, element, attr, $compile, FormService) {        
            this.element = element;          
            this.form_bg = element.find(".fwr-form-bg");
            this.form_fg = element.find(".fwr-form-fg");
            this.form_bg_img = element.find("img");          
            
            var formWidget = $scope.formWidget;

            if (formWidget.bMultiLine) {
                element.find("input").remove();
                this.form_input = element.find("textarea");
            } else {
                element.find("textarea").remove();
                this.form_input = element.find("input");
            }
            if (formWidget.bReadOnly) {
                element.css("pointer-events", "none");
                this.form_input.attr("tabindex", -1);
            }
            if (formWidget.isPassword){
                this.form_input.attr("type", "password");
            } 

            var self = this;
            $scope.$watch('formWidget.onfocus', function(newValue, oldValue) {
                if (newValue === oldValue) return;
                //onBlur
                if (!newValue) {
                    if (!formWidget.validateInputValue(self.form_input.val(), element)) return;
                    FormService.setItemValue(formWidget.name, self.form_input.val());
                    self.form_input.val(formWidget.formateViewValue(form_input.val()));
                } else {
                    self.form_input.val(FormService.getItemValue(formWidget.name));
                }
            });

            FormService.onItemValueChange($scope.formWidget.name, function() {
                self.form_input.val(formWidget.formateViewValue(FormService.getItemValue(formWidget.name)));
            });
        },
        layout: function($scope) {          
            var formWidget = $scope.formWidget;
            var scale = $scope.scale;
            var divBgLeft = transferPage(formWidget.rect.left, scale);
            var divBgTop = transferPage(formWidget.rect.top, scale);
            var divBgWidth = transferPage(formWidget.rect.width, scale);
            var divBgHeight = transferPage(formWidget.rect.height, scale);
            var divBorderWidth = transferPage(formWidget.borderWidth, scale);
            var divLeft = divBorderWidth;
            var divTop = divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var divFontSize = 0;
            if (formWidget.fontSize != -1) {
                divFontSize = transferPage(formWidget.fontSize, scale);
            } else {
                if (formWidget.bMultiLine) {
                    divFontSize = transferPage(12, scale);
                } else {
                    if (formWidget.formPage.rotate == 1 || formWidget.formPage.rotate == 3) {
                        divFontSize = transferPage(formWidget.rect.width * 0.7, scale);
                    } else {
                        divFontSize = transferPage(formWidget.rect.height * 0.7, scale);
                    }
                }
            }
            setStyleLTWH(this.element, divBgLeft, divBgTop, divBgWidth, divBgHeight);
            if (formWidget.bMultiLine) {
                this.form_input.css('max-width', divWidth + 'px').css('max-height', divHeight + 'px').css('font-size', divFontSize + 'px');
                setStyleLTWH(this.form_input, divLeft, divTop, divWidth, divHeight);
            } else {
                this.form_input.css('line-height', divHeight + 'px').css('font-size', divFontSize + 'px');
                setStyleLTWH(this.form_input, divLeft, divTop, divWidth, divHeight);
            }

            var imgLeft = 0 - transferPage(formWidget.imgPos, scale, 1);
            var imgWidth = transferPage(formWidget.formPage.allImgWidth, scale, 1);
            var imgHeight = transferPage(formWidget.formPage.allImgHeight, scale, 1);
            setStyleLTWH(this.form_bg_img, imgLeft, 0, imgWidth, imgHeight);          
        }       
    }));
});